<template>
  <div>
    <!--对象搜索-->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="产品名称" prop="product">
        <y-select
          v-model="queryParams.product"
          placeholder="请选择产品名称"
          :optionsFunc="listProduct"
        />
      </el-form-item>
      <el-form-item label="报文名称" prop="packet">
        <y-select
          v-model="queryParams.packet"
          placeholder="请选择报文名称"
          :optionsFunc="listPacketType"
          :optionsParams="{ product: queryParams.product, nopaginate: true }"
        />
      </el-form-item>
      <el-form-item label="字段名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入字段名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--对象操作-->
    <el-row :gutter="10" class="mb8">
      <y-right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      />
    </el-row>

    <!--对象列表-->
    <el-table
      ref="bindTableRef"
      v-loading="loading"
      row-key="id"
      :data="fieldList"
      @selection-change="handleSelectionChange"
      border
      highlight-current-row
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        reserve-selection
      />
      <el-table-column label="序号" width="100" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="报文名称"
        align="center"
        prop="packet.label"
        v-if="columns[0].visible"
        sortable
      />
      <el-table-column
        label="字段名称"
        align="center"
        prop="name"
        v-if="columns[1].visible"
        sortable
      />
      <el-table-column
        label="字段描述"
        align="center"
        prop="desc"
        v-if="columns[2].visible"
        sortable
      />
      <el-table-column
        label="字段类型"
        align="center"
        prop="type"
        v-if="columns[3].visible"
        sortable
      />
      <el-table-column
        label="字段长度"
        align="center"
        prop="length"
        v-if="columns[4].visible"
        sortable
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="create_time"
        v-if="columns[5].visible"
        sortable
        width="160"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!--列表分页-->
    <y-pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="PacketFieldBind">
import { listProduct } from "@/api/pms/product";
import { listPacketType } from "@/api/rms/packet/type";
import { listPacketField } from "@/api/rms/packet/field";

const { proxy } = getCurrentInstance();

const binding = ref([]);
const loading = ref(false);
const showSearch = ref(false);
const total = ref(0);
const fieldList = ref([]);
const dateRange = ref([]);

// 列显隐信息
const columns = ref([
  { key: 0, label: `报文名称`, visible: true },
  { key: 1, label: `字段名称`, visible: true },
  { key: 2, label: `字段描述`, visible: true },
  { key: 3, label: `字段类型`, visible: true },
  { key: 4, label: `字段长度`, visible: true },
  { key: 5, label: `创建时间`, visible: true },
]);

const data = reactive({
  queryParams: {
    product: undefined,
    packet: undefined,
    name: undefined,
    page: 1,
    limit: 10,
  },
});

const { queryParams } = toRefs(data);

/** 查询报文类型列表 */
function getList() {
  loading.value = true;
  listPacketField(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      fieldList.value = res.data;
      total.value = res.total;
      loading.value = false;
    }
  );
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  binding.value = selection.map((item) => item);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** Expose方法 */
defineExpose({
  handleQuery,
  binding,
});
</script>
